import { Component, OnInit } from '@angular/core';
import { ShopService } from 'src/app/services/shop.service';
import { ToastService } from 'src/app/providers/toast/toast.service';
import { NavController } from '@ionic/angular';
import { ActivatedRoute, Params } from '@angular/router';
import * as constants from 'src/app/providers/Constants';
import { Storage } from 'src/app/providers/Storage';

@Component({
  selector: 'app-goods-filtrate',
  templateUrl: './goods-filtrate.page.html',
  styleUrls: ['./goods-filtrate.page.scss'],
})
export class GoodsFiltratePage implements OnInit {
  activeKey2: number = 0;

  activeKey3: number = 0;
  activeKey4: number = 0;
  measureList: any = [];
  colorList: any = [];
  brandList: any = [];
  dataList: any = [];
  categoryData: any = [];

  minMoney: number = null;
  MaxMoney: number = null;
  brandIndex: number = null;
  categoryDataIndex: number = null;
  measureIndex: number = null;
  colorIndex: number = null;
  constructor(private nav: NavController, private activeRoute: ActivatedRoute, public service: ShopService, private toast: ToastService) {


  }

  ngOnInit() {
    this.getList();
  }
  getList() {
    this.service.getProcStandards({}, { showLoading: false }).subscribe(res => {
      // 保存成功返沪上一页，否则弹出提示框
      if (res.retCode == '000000') {
        let idxList = Storage.sessionStorage.get('idxList');
        if (idxList) {
          if (idxList.brandIndex) {
            this.brandIndex = idxList.brandIndex;
            res.procBrand[idxList.brandIndex].select = true;
          }
          if (idxList.measureIndex) {
            this.measureIndex = idxList.measureIndex;
            res.procStandards[idxList.measureIndex].select = true;
          }
          if (idxList.colorIndex) {
            this.colorIndex = idxList.colorIndex;
            res.procColor[idxList.colorIndex].select = true;
          } if (idxList.lowPrice) {
            this.minMoney = idxList.lowPrice;
          } if (idxList.heightPrice) {
            this.MaxMoney = idxList.heightPrice;
          }
        }
          this.dataList = res;
       this.queryProcClass();

      } else if (constants.errCode[res.retCode]) {
        if (constants.errCode[res.retCode] != '0') {
          this.toast.show(constants.errCode[res.retCode]);
        }
      } else if (res.retMsg) {
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    });
  }
  close() {
    console.log('this.minMoney=', this.minMoney);
    console.log('this.MaxMoney=', this.MaxMoney);
    if (this.minMoney > this.MaxMoney) {
      // this.toast.show('最低价不能大于最高价');
      this.toast.show('Price is wrong');
      return;
    }
    let idxList = {
      brandIndex: this.brandIndex,
      categoryDataIndex: this.categoryDataIndex,
      measureIndex: this.measureIndex,
      colorIndex: this.colorIndex,
      lowPrice: this.minMoney,
      heightPrice: this.MaxMoney,
    }

    let brandName = '';
    let measureName = '';
    let colorName = '';
    let procClassId = '';
    if (this.brandIndex != null) {
      console.log('this.brandIndex =', this.brandIndex);
      brandName = this.dataList.procBrand[this.brandIndex].procBrandName;
    }
    if (this.measureIndex != null) {
      console.log('this.measureIndex =', this.measureIndex);
      measureName = this.dataList.procStandards[this.measureIndex].procStandards;
      // measureName = this.measureList[this.measureIndex].procStandards;
    }
    if (this.colorIndex != null) {
      console.log('this.colorIndex =', this.colorIndex);
      colorName = this.dataList.procColor[this.colorIndex].procColor;
    } if (this.categoryDataIndex != null) {
      console.log('this.colorIndex =', this.categoryDataIndex);
      procClassId = this.dataList.categoryData[this.categoryDataIndex].procClassId;
    }
    let filtrateData = {
      brandName: brandName,
      measureName: measureName,
      colorName: colorName,
      procClassId: procClassId,
      lowPrice: this.minMoney,
      heightPrice: this.MaxMoney,
    }
    Storage.sessionStorage.set('idxList', idxList);
    Storage.sessionStorage.set('filtrateData', filtrateData);
    // this.nav.navigateBack('goods-search-list');
    this.nav.pop();
  }
  onProcBrand(idx) {
    for (let index = 0; index < this.dataList.procBrand.length; index++) {
      if (index == idx) {
        this.dataList.procBrand[index].select = true;
        this.brandList[index].select = true;
        this.brandIndex = index;
      } else {
        this.dataList.procBrand[index].select = false;
        this.brandList[index].select = false;
      }

    }
    console.log(this.brandList);
  }
  onCategoryData(idx) {
    for (let index = 0; index < this.dataList.categoryData.length; index++) {
      if (index == idx) {
        this.dataList.categoryData[index].select = true;
        this.categoryData[index].select = true;
        this.categoryDataIndex = index;
      } else {
        this.dataList.categoryData[index].select = false;
        this.categoryData[index].select = false;
      }
    }


    console.log(this.brandList);
  }
  onMeasure(idx) {
    console.log(this.dataList);
    for (let index = 0; index < this.dataList.procStandards.length; index++) {
      if (idx == index) {
        this.dataList.procStandards[index].select = true;
        this.measureList[index].select = true;
        this.measureIndex = idx;
      } else {
        this.dataList.procStandards[index].select = false;
        this.measureList[index].select = false;
      }

    }

  }
  queryProcClass() {
    this.service.queryProcClass({
      // mode: m,
      procParentClassId: '',
      type: '1',
    }).subscribe(res => {
      console.log(JSON.stringify(res));
      if (res.retCode == '000000') {
        let idxList = Storage.sessionStorage.get('idxList');
        if (idxList) {
          if (idxList.categoryDataIndex) {
            this.categoryDataIndex = idxList.categoryDataIndex;
            res.data[idxList.categoryDataIndex].select = true;
          }
        }
        this.dataList.categoryData = res.data;
        console.log(' this.dataList.categoryData', this.dataList.categoryData);
        
        // this.categoryData = res.data;

      } else {
        this.toast.show("exception service routine");
      }
    })
  }
  onColorList(index) {
    if (this.colorIndex != null) {
      this.dataList.procColor[this.colorIndex].select = false;
      this.colorList[this.colorIndex].select = false;
    }
    this.dataList.procColor[index].select = true;
    this.colorList[index].select = true;
    this.colorIndex = index;
  }

  openItem(key) {
 

    if(key==0){ 
      this.activeKey2 = this.activeKey3= this.activeKey4=0;
      this.clearData(key)
    }else if(key==2){
      if(this.activeKey2 == key){
        this.activeKey2 = 0;
        this.clearData(key)
      }else{
        this.activeKey2=key
        this.addData(key)
        return
      }

    }else if(key==3){
      if(this.activeKey3 == key){
        this.activeKey3= 0;
        this.clearData(key)
      }else{
        this.activeKey3=key
         this.addData(key)
        return
      }
    }else if(key==4){
      if(this.activeKey4 == key){
        this.activeKey4= 0;
        this.clearData(key)
      }else{
        this.activeKey4=key
        this.addData(key)
        return
      }
    } 
return

    if (this.activeKey2 == key || key == 0) {
      this.activeKey2 = 0;
      this.measureList = [];
      this.brandList = [];
      this.colorList = [];
      this.minMoney = null;
      this.MaxMoney = null;
      this.categoryData = [];
      if (this.brandIndex) {
        this.dataList.procBrand[this.brandIndex].select = false;
        this.brandIndex = null;
      }

      if (this.measureIndex) {
        this.dataList.procStandards[this.measureIndex].select = false;
        this.measureIndex = null;
      } 

      if (this.colorIndex) {
        this.dataList.procColor[this.colorIndex].select = false;
        this.colorIndex = null;
      }
      if (this.categoryDataIndex) {
        this.dataList.categoryData[this.categoryDataIndex].select = false;
        this.categoryDataIndex = null;
      }

      Storage.sessionStorage.remove('idxList');
      Storage.sessionStorage.remove('filtrateData');
    } else {
      this.activeKey2 = key;
      this.measureList = [];
      this.brandList = [];
      this.colorList = [];
      this.categoryData = [];

      if (key == 1) {
        this.measureList = this.dataList.procStandards;
      } else if (key == 2) {
        this.brandList = this.dataList.procBrand;
      } else if (key == 3) {
        this.colorList = this.dataList.procColor;
      } else if (key == 4) {
        console.log(' this.dataList.categoryData', this.dataList);
        console.log(' this.categoryData', this.categoryData);
        this.categoryData = this.dataList.categoryData;
      }
    }
  }

  addData(key){



    
      
       if (key == 2) {
        this.brandList = [];
        this.brandList = this.dataList.procBrand;
      } else if (key == 3) {
        this.colorList = [];
        this.colorList = this.dataList.procColor;
      } else if (key == 4) {
        this.categoryData = [];
        console.log(' this.dataList.categoryData', this.dataList);
        console.log(' this.categoryData', this.categoryData);
        this.categoryData = this.dataList.categoryData;
      }
  }

  clearData(key){
    if(key==2){
      this.brandList = [];
    }else if(key==3){
      this.colorList = [];
    }else if(key==4){
      this.categoryData = [];
    }else if(key==0){
      this.measureList = [];
      this.brandList = [];
      this.colorList = [];
      this.minMoney = null;
      this.MaxMoney = null;
      this.categoryData = [];
      if (this.brandIndex) {
        this.dataList.procBrand[this.brandIndex].select = false;
        this.brandIndex = null;
      }
  
      if (this.measureIndex) {
        this.dataList.procStandards[this.measureIndex].select = false;
        this.measureIndex = null;
      } 
  
      if (this.colorIndex) {
        this.dataList.procColor[this.colorIndex].select = false;
        this.colorIndex = null;
      }
      if (this.categoryDataIndex) {
        this.dataList.categoryData[this.categoryDataIndex].select = false;
        this.categoryDataIndex = null;
      }
  
      Storage.sessionStorage.remove('idxList');
      Storage.sessionStorage.remove('filtrateData');
    }
   
    // this.measureList = [];
    // this.brandList = [];
    // this.colorList = [];
    // this.minMoney = null;
    // this.MaxMoney = null;
    // this.categoryData = [];
    // if (this.brandIndex) {
    //   this.dataList.procBrand[this.brandIndex].select = false;
    //   this.brandIndex = null;
    // }

    // if (this.measureIndex) {
    //   this.dataList.procStandards[this.measureIndex].select = false;
    //   this.measureIndex = null;
    // } 

    // if (this.colorIndex) {
    //   this.dataList.procColor[this.colorIndex].select = false;
    //   this.colorIndex = null;
    // }
    // if (this.categoryDataIndex) {
    //   this.dataList.categoryData[this.categoryDataIndex].select = false;
    //   this.categoryDataIndex = null;
    // }

    // Storage.sessionStorage.remove('idxList');
    // Storage.sessionStorage.remove('filtrateData');



  }

  sub(){
    this.nav.pop();
  }
}
